<?php

App::uses('AppController', 'Controller');

/**
 * TypeProducts Controller
 *
 * @property TypeProduct $TypeProduct
 * @property PaginatorComponent $Paginator
 */
class TypeProductsController extends AppController {

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
       $this->TypeProduct->recursive = 0; 
       if ($this->request->is('post')) {
            $text = $this->request->data['search_text'];
            $this->Paginator->settings=array(
                'conditions'=>array('TypeProduct.type_name LIKE'=>"%$text%"),
                'order'=>array('TypeProduct.type_name ASC'),
                'limit'=>5
            );
        }
        else{
            $this->Paginator->settings=array(
                'order'=>array('TypeProduct.parent_id DESC','TypeProduct.type_name ASC'),
                'limit'=>5
            );
        }
        
        $this->set('typeProducts', $this->Paginator->paginate());
        
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->TypeProduct->exists($id)) {
            throw new NotFoundException(__('Invalid type product'));
        }
        $options = array('conditions' => array('TypeProduct.' . $this->TypeProduct->primaryKey => $id));
        $this->set('typeProduct', $this->TypeProduct->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->TypeProduct->create();
            $this->request->data['TypeProduct']['create_date'] = date("Y-m-d");
            $this->request->data['TypeProduct']['create_person'] = $this->Session->read("userName");
            if ($this->TypeProduct->save($this->request->data)) {
                $this->Session->setFlash(__('Thêm thành công '), 'backend/green', array('redirect' => ''));
                return $this->redirect(array('action' => 'admin_index'));
            } else {
                $this->Session->setFlash(__('Lỗi save dữ liệu'), 'backend/red', array('redirect' => ''));
            }
        }
        $typeProduct = $this->TypeProduct->find("list", array("conditions" => array('TypeProduct.parent_id' => ''), "fields" => array('TypeProduct.id', 'TypeProduct.type_name')));
        $this->set(compact('typeProduct'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        if (!$this->TypeProduct->exists($id)) {
            throw new NotFoundException(__('Invalid type product'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->TypeProduct->save($this->request->data)) {
                $this->Session->setFlash(__('Sửa thành công '), 'backend/green', array('redirect' => ''));
                return $this->redirect(array('action' => 'admin_index'));
            } else {
                $this->Session->setFlash(__('Lỗi save dữ liệu'), 'backend/red', array('redirect' => ''));
            }
        } else {
            $options = array('conditions' => array('TypeProduct.' . $this->TypeProduct->primaryKey => $id));
            $this->request->data = $this->TypeProduct->find('first', $options);
            $typeProduct = $this->TypeProduct->find("list", array("conditions" => array('TypeProduct.parent_id' => ''), "fields" => array('TypeProduct.id', 'TypeProduct.type_name')));
            $this->set(compact('typeProduct'));
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        $this->TypeProduct->id = $id;
        if (!$this->TypeProduct->exists()) {
            throw new NotFoundException(__('Invalid tranning program'));
        }
        $this->request->onlyAllow('get', 'admin_delete');

        if ($this->TypeProduct->delete()) {
            $this->Session->setFlash(__('Xóa thành công'), 'backend/green', array('redirect' => ''));
        } else {
            $this->Session->setFlash(__('Lỗi xóa dữ liệu'), 'backend/red', array('redirect' => ''));
        }
        return $this->redirect(array('action' => 'admin_index'));
    }
    public function getTypeName($id)
    {
        $typeProduct = $this->TypeProduct->find('first',array('conditions'=>array('TypeProduct.id'=>$id),'fields'=>array('TypeProduct.type_name')));
        if(!empty($typeProduct))
        {
            return $typeProduct['TypeProduct']['type_name'];
        }
        return '';
    }
    public function getTypes($id)
    {
        $this->autoRender=false;
        $this->layout=false;
        $typeProduct = $this->TypeProduct->find('list',array('conditions'=>array('TypeProduct.type_fashion'=>$id),'fields'=>array('TypeProduct.id','TypeProduct.type_name')));
        $option = '<option value=""> Chọn loại sản phẩm cha </option>';
        if(!empty($typeProduct))
        {
            
            foreach($typeProduct as $k=>$v)
            {
               $option .='<option value="'.$k.'">'.$v.'</option>'; 
            }
        }
        return $option;
    }

}
